package com.gdk.ksoft.ui.reporters.adapters;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gdk.ksoft.R;

import java.util.HashMap;
import java.util.List;

/**
 * Created by hel on 2014-12-19.
 * 商品库存查询数据适配器
 */
public class GoodsStockAdapter extends BaseAdapter {

    private Context context;
    private List<HashMap<String,Object>> list;

    public GoodsStockAdapter(Context context,List<HashMap<String,Object>> list){
        this.context  = context;
        this.list = list;
    }


    @Override
    public int getCount() {
        return list == null?0:list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return list.size();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final HashMap<String,Object> item = list.get(position);
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_tbl_goods_stock_info, null);
            holder.txtName = (TextView) convertView.findViewById(R.id.item_tbl_goods_stock_info_txtName);
            holder.txtJs = (TextView) convertView.findViewById(R.id.item_tbl_goods_stock_info_txtJs);
            holder.txtSs = (TextView) convertView.findViewById(R.id.item_tbl_goods_stock_info_txtSs);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.txtName.setText(item.get("gmc").toString());
        final int intKc = Integer.parseInt(item.get("gkc").toString());
        final int intHsgx = Integer.parseInt(item.get("ghsgx").toString());
        final int intJs = intKc/intHsgx;
        final int intSs = intKc%intHsgx;
        holder.txtJs.setText(intJs == 0?"":String.valueOf(intJs));
        holder.txtSs.setText(intSs == 0?"":String.valueOf(intSs));
        holder.txtJs.setTextColor(intJs < 0?Color.RED:Color.DKGRAY);
        holder.txtSs.setTextColor(intSs < 0?Color.RED:Color.DKGRAY);
        return convertView;
    }

    class ViewHolder{
        public TextView txtName;
        public TextView txtJs;
        public TextView txtSs;
    }

}
